﻿using JMBox.Utils;
using static JMBox.Config.R;

namespace JMBox.Task
{
    public class ClearTask
    {
        public static int SpaceCount = 0;
        //任务运行标志
        public static bool RunTask = false;


        /// <summary>
        /// 工作内容
        /// </summary>
        private void InnerRun()
        {
            if (!MacCheckTool.CanUseBat())
            {
                MessageBox.Show("未认证");
                return;
            }
            if (!GlobalConfig.IsAlive())
            {
                MessageBox.Show("已过期");
                return;
            }
            while (RunTask && GlobalStatus.AllFunOpenFlag && GlobalStatus.AppIsRuning)
            {
                if (GlobalStatus.OpenClear)
                {
                    int[] duration = RandomNumTool.GenerateRandomNumbers(GlobalConfig.ClearRandomMin, GlobalConfig.ClearRandomMax, 1);
                    try
                    {
                        SpaceCount++;
                        Contact.MainUI.GetPressKey().KeyPress(Keys.Space);
                        Thread.Sleep(duration[0]);

                        //超过设置的阈值，则自动关闭
                        if (SpaceCount > GlobalConfig.ClearDuration)
                        {
                            SpaceCount = 0;
                            GlobalStatus.OpenClear = false;
                            RunTask = false;
                        }
                    }
                    catch { }
                }
            }
        }

        /// <summary>
        /// 开始执行任务
        /// </summary>
        public void Start()
        {
            if (ClearTask.RunTask && GlobalStatus.OpenClear)
            {
                Thread t = new Thread(InnerRun);
                t.Start();
            }
        }

        /// <summary>
        /// 结束任务
        /// </summary>
        public void End()
        {
            RunTask = false;
        }
    }
}